package top.i51213.fastaction.action.image;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import java.util.List;

import top.i51213.fastaction.action.BaseActonComponent;
import top.i51213.fastaction.action.Data;
import top.i51213.fastaction.action.DataType;
import top.i51213.fastaction.action.IntentType;
import top.i51213.fastaction.uri.MimeType;
import top.i51213.fastaction.uri.UriReader;
import top.i51213.fastaction.util.IOUtil;
import top.i51213.fastaction.uri.UriWriter;
import top.i51213.fastaction.R;
import top.i51213.fastaction.util.Util;

public class ImageCompressActonComponent extends BaseActonComponent {
    ImageCompressor.LimitRectSize limitRectSize;
    View editView;
    EditText et_limit;

    public ImageCompressActonComponent(Context context, ImageCompressor.LimitRectSize limitRectSize) {
        super(context);
        this.limitRectSize = limitRectSize;
        editView = LayoutInflater.from(getContext()).inflate(R.layout.editor_compree_setting, null);
        et_limit = editView.findViewById(R.id.editor_compress_et_limit);
    }

    @Override
    protected Data makeOutputData() {
        return compressAll();
    }

    private Data compressAll() {
        Data compressData = new Data(DataType.IMAGE);
        List<Uri> list = getInputData().getUris();
        if (getInputData().getDataType() == DataType.IMAGE) {
            int limit = Util.tryParseInt(et_limit.getText().toString(), 3096) * 1024;
            for (Uri uri : list) {
                try {
                    UriReader uriReader = new UriReader(getContext(), uri);
                    String displayName = IOUtil.getFileNameNoExt(uriReader.getDisPlayName()) + MimeType.Image.Jpeg.getExt();
                    UriWriter uriWriter = new UriWriter(getContext(), displayName, MimeType.Image.Jpeg, true);
                    ImageCompressor.compressJpeg(uriReader, uriWriter, limitRectSize, limit);
                    compressData.addData(DataType.IMAGE, uriWriter.getUri());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (getInputData().getDataType() == DataType.PdfFile) {
            //int limit = Util.tryParseInt(et_limit.getText().toString(), 3096) * 1024;
            for (Uri uri : list) {
                try {
                    UriReader uriReader = new UriReader(getContext(), uri);
                    List<Uri> images = ImageCompressor.pdf2Jpeg2(uriReader, limitRectSize);
                    compressData.addData(DataType.IMAGE, images);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return compressData;
    }

    @Override
    public DataType getOutputDataType() {
        return DataType.IMAGE;
    }


    @Override
    public IntentType getCurrentIntentType() {
        return IntentType.ImageCompress;
    }

    @Override
    public int getImageResId() {
        return R.mipmap.ic_launcher;
    }

    @Override
    public String getTitle() {
        return "图片压缩" + limitRectSize.getTitle();
    }

    @Override
    public String getInfo() {
        return "支持Jpeg、Png、Webp、Pdf\n存储大小限制:" + et_limit.getText().toString() + "K";
    }

    @Override
    public View getEditView() {
        return editView;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void editViewDismissListener() {
        int v = Util.tryParseInt(et_limit.getText().toString(), -1);
        if (v < 1) {
            et_limit.setText("1");
        }
    }
}
